import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:u_push_plugin/u_push_plugin.dart';
import 'package:u_push_plugin/data.dart';
import 'package:u_push_plugin_example/test_message_data.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String deviceToken = "";
  String message = "";

  @override
  void initState() {
    super.initState();
    UPushPlugin.initMessageListener();
    UPushPlugin.initRegisterListener((value) {
      deviceToken = value.message;
      setState(() {});
    });
    UPushPlugin.initMessageResultListener((value) {
      MessageData(value);
      setState(() {
        message = MessageData(value).message;
      });
    });
    UPushPlugin.getCacheMessage().then((value) {
      if(value.isNotEmpty){
        setState(() {
          message = MessageData(value).message;
        });
      }
    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
//    String platformVersion;
//    if (!mounted) return;
//
//    setState(() {
//      _platformVersion = platformVersion;
//    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: <Widget>[
            Text(deviceToken),
            Text(message),
            FlatButton(
                onPressed: () async {
                  deviceToken = await UPushPlugin.getDeviceToken();
                  setState(() {});
                },
                child: Text("获取登录信息"))
          ],
        ),
      ),
    );
  }
}
